<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\AdminUser;
use GuzzleHttp\Client;
use App\Models\msg;

class BandController extends Controller
{
    public  function index(Request $request) {
        $DeviceID=$request->get('DeviceID');
        $username=$request->get('username');
        $password=$request->get('password');
        if(!$username||!$DeviceID||!$password){ 
           return json_encode(['code'=>0,'date'=>'','msg'=>'参数不完善']);
        }
        
        
        $http = new Client;
        
            $response = $http->post(config('app.url').'/oauth/token', [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' =>'932b0b78-2f99-411f-b82b-1e1a4d97185b',
                    'client_secret' =>'cPVUPb3pESC7wtNu9xs0VPN0QVOqo7mTJNdJQjHA',
                    'username' => $username,
                    'password' => $password,
                    'scope' => '*',
                ],
            ]);
          unset($http);
        //$status = $response->getStatusCode();
      //  $body = $response->getBody();

      //  info($body->getContents());
      //  info($status);   
            
            
 
        $token=json_decode((string)$response->getBody(), true);
        if(!empty($token['error'])){
            return response()->json(['code'=>0,'date'=>'','msg'=>'账号密码不对或过期']); 
        }
        $user=AdminUser::where('email',$username)->first();
        if($user->expired_at&&$user->expired_at<Carbon::now()){//过期
             return response()->json(['code'=>0,'date'=>'','msg'=>'账号过期']); 
        }
        if(!$user->name){//绑定
            $user->name=$DeviceID;
            $user->expired_at=Carbon::now()->addDays($user->day);
            $user->save();
            $user->name='';
            return response()->json(['code'=>1,'date'=>['info'=>$user,'token'=>$token],'msg'=>'']);
        }else{
            if($DeviceID==$user->name){
                $user->name='';
                return response()->json(['code'=>1,'date'=>['info'=>$user,'token'=>$token],'msg'=>'']);
            }else{
                return response()->json(['code'=>0,'date'=>'','msg'=>'此账号绑定过设备！不能更换']); 
            }
        }
    }
    public function verification(Request $request){
        $user=$request->user();
        $lo=$request->get('lo');
        $la=$request->get('la');
        $ip=$request->getClientIp();
        AdminUser::where('id',$user->id)->update(['remember_token'=>$ip,'updated_at'=>date('Y-m-d:H:i:s')]);
        $expired_at=AdminUser::where('id',$user->id)->value('expired_at');
        if($expired_at<Carbon::now()){//过期
            return response()->json(['code'=>0,'date'=>'','msg'=>'账号过期']); 
        }
        $msg=msg::where('uid',$user->id)->where('is_default',0)->first();
        if($msg){
            return response()->json(['code'=>8,'date'=>$msg,'msg'=>'']);  
        }
        
        return response()->json(['code'=>1,'date'=>$user,'msg'=>'']); 
    }
    public function login(Request $request){
        return response()->json(['code'=>2,'date'=>'','msg'=>'请从新登陆']); 
    }
}
